package com.woniu.service.impl;

import com.woniu.entity.Customer;
import com.woniu.mapper.CustomerMapper;
import com.woniu.service.CustomerService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;

    @Override
    public ResponseUtil findAll() {
        List<Customer> all = customerMapper.findAll();
        return !all.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, all)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }


    @Override
    public ResponseUtil addCustomer(Customer customer) {
        Customer customerByAccount = customerMapper.selectByAccount(customer.getUsername());
        if (customerByAccount== null) {
            customer.setStatus(1);
            int result = customerMapper.add(customer);
            return ResponseUtil.get(ResponseEnum.SUCCESS);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    @Override
    public ResponseUtil deleteCustomer(Integer customerId) {
        // 检查是否被引用
        int count = customerMapper.checkReference(customerId);
        if (count > 0) {
            return ResponseUtil.get(ResponseEnum.REFERENCED);
        }

        int result = customerMapper.delete(customerId);
        if (result > 0) {
            return ResponseUtil.get(ResponseEnum.SUCCESS);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    @Override
    public ResponseUtil updateCustomer(Customer customer) {
        int result = customerMapper.update(customer);
        if (result > 0) {
            return ResponseUtil.get(ResponseEnum.SUCCESS);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    @Override
    public ResponseUtil getCustomerById(Integer customerId) {
        Customer customer = customerMapper.findById(customerId);
        if (customer != null) {
            return ResponseUtil.get(ResponseEnum.SUCCESS, customer);
        } else {
            return ResponseUtil.get(ResponseEnum.NOT_FOUND);
        }
    }

    @Override
    public List<Customer> getAllCustomers() {
        return customerMapper.findAll();
    }

    @Override
    public List<Customer> searchCustomers(String keyword) {
        return customerMapper.findByLike(keyword);
    }

    @Override
    public ResponseUtil disableCustomer(Integer customerId) {
        // 检查是否被引用
        int count = customerMapper.checkReference(customerId);
        if (count > 0) {
            return ResponseUtil.get(ResponseEnum.REFERENCED);
        }

        int result = customerMapper.updateStatus(customerId, 0);
        if (result > 0) {
            return ResponseUtil.get(ResponseEnum.SUCCESS);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }

    @Override
    public ResponseUtil enableCustomer(Integer customerId) {
        int result = customerMapper.updateStatus(customerId, 1);
        if (result > 0) {
            return ResponseUtil.get(ResponseEnum.SUCCESS);
        } else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
    }
}
